<?php

namespace App\Jobs;

use App\Models\Manufacturer;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class SendSMSJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(public Manufacturer $info, public String $title)
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 发短信
        $resp = Http::withOptions([
            'verify' => false,
        ])->post('https://hmpublic.huimaisoft.com:2044/api/notice/sms', [
            'identification_name' => 'Ydj6cEuyEl',
            'api_key' => 'jy44mDyVTmTYgCzOWjCT',
            'api_secret' => 'sKB1LBrNnwgVYjif6itxegeiXjXbVCTYuQocHofe',
            'authorization_type' => 'sms',
            'type' => 'meeting_invite',
            'phones' => $this->info->mobile,
            'params' => json_encode([
                'name' => $this->info->name,
                'meeting_name' => $this->title,
                'people_name' => $this->info->person->nickname,
            ]),
        ]);
        $res = json_decode($resp->body(), true);
        if ($res && $res['code'] && $res['code'] == 200) {
            $this->info->is_send = 1;
            $this->info->send_data = json_encode($res, JSON_UNESCAPED_UNICODE);
        } else {
            $this->info->is_send = -1;
            $this->info->send_data = json_encode($res, JSON_UNESCAPED_UNICODE);
        }
        $this->info->save();
        Log::info('发送邀请短信', ['data' => $this->info->id]);
    }
}
